#include "dialog.h"
#include "ui_dialog.h"
#include <QStandardItem>

#include "sqlitedboperator.h"
#include "all_drive.h"


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <fcntl.h>                          // 提供open()函数
#include <sys/types.h>                      // 提供mode_t类型
#include <sys/stat.h>                       // 提供open()函数的符号

SqliteDBAOperator MyDB_light;

extern int light_arr[5];
extern int kbdfd,ledsfd,beepfd,adcfd;
all_drive a_dr;
unsigned int page=0;





Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    dialog_init();
}

Dialog::~Dialog()
{
    qDebug()<<"ookk";
    delete ui;
}

enum {BEDROOM1=0,LIVROOM=1,KITCHEN=2,BEDROOM2=3};


void Dialog::dialog_init()
{
    this->setWindowState(Qt::WindowNoState);

    Image_lampson.load(":/images/lampleton.png");
    pixmap_lampson = QPixmap::fromImage(Image_lampson);
    fitpixmap_lampson = pixmap_lampson.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    Image_lampsoff.load(":/images/lampletoff.png");
    pixmap_lampsoff = QPixmap::fromImage(Image_lampsoff);
    fitpixmap_lampsoff = pixmap_lampsoff.scaled(64, 64, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    //打开数据库
    MyDB_light.OpenDb();
    tablename="EVENTTABLE_LIGHT";
    MyDB_light.createTable();
    queryTable();

    ui->stackedWidget->setCurrentIndex(page);

    kitchen_light = light_arr[0];
    livingroom_light =light_arr[1];
    bed_room2_light=light_arr[2];
    bed_room1_light=light_arr[3];
    char buff=0;
    read(ledsfd,&buff,1);
    if(buff&0x01) kitchen_light=true; else kitchen_light=false;
    if(buff&0x02) livingroom_light=true; else livingroom_light=false;
    if(buff&0x04) bed_room1_light=true; else bed_room1_light=false;
    if(buff&0x08) bed_room2_light=true; else bed_room2_light=false;



    setpage_off(0);
    setpage_off(1);
    setpage_off(2);
    setpage_off(3);
    //todo: led set
    if(kitchen_light==true)
    {
       a_dr.LEDsSetValue(1,0);
       setpage_on(KITCHEN);
    }
    if(livingroom_light==true)
    {
        a_dr.LEDsSetValue(1,1);
        setpage_on(LIVROOM);
    }
    if(bed_room1_light==true)
    {
        a_dr.LEDsSetValue(1,2);
        setpage_on(BEDROOM1);
    }
    if(bed_room2_light==true)
    {
        a_dr.LEDsSetValue(1,3);
        setpage_on(BEDROOM2);
    }

    KbdScanTimer = new QTimer(this);//创建一个定时器
    connect(KbdScanTimer, SIGNAL(timeout()), this, SLOT(deal_key()));//链接按键扫描定时器信号给KbdLedScan函数

    if(kbdfd>0)
        KbdScanTimer->start(100);//开启100ms的按键和LED状态扫描

    SwitchUITimer = new QTimer(this);
    connect(SwitchUITimer,&QTimer::timeout,this,&Dialog::switchuicallback);
    SwitchUITimer->start(10);
}
void Dialog::queryTable()
{
    QString str = QString(" select *from EVENTTABLE_LIGHT");
    sqlmodel.setQuery(str);
    ui->tableView->setModel(&sqlmodel);
}

void Dialog::setpage_off(int p)
{

    switch (p)
    {
    case BEDROOM2:
        ui->page0->setStyleSheet("border-image: url(:/images/bedroom1-off.jpeg);");
        break;
    case LIVROOM:
        ui->page1->setStyleSheet("border-image: url(:/images/living-room-off.jpg);");
        break;
    case KITCHEN:
        ui->page2->setStyleSheet("border-image: url(:/images/kitchen-off.jpg);");
        break;
    case BEDROOM1:
        ui->page3->setStyleSheet("border-image: url(:/images/bedroom2-off.jpeg);");
        break;
    }
}

void Dialog::setpage_on(int p)
{
    qDebug()<<"on:"<<p<<endl;
    switch (p)
    {
    case BEDROOM2:
        ui->page0->setStyleSheet("border-image: url(:/images/bedroom1.jpeg);");
        break;
    case LIVROOM:
        ui->page1->setStyleSheet("border-image: url(:/images/living-room.jpg);");
        break;
    case KITCHEN:
        ui->page2->setStyleSheet("border-image: url(:/images/kitchen.jpg);");
        break;
    case BEDROOM1:
        ui->page3->setStyleSheet("border-image: url(:/images/bedroom2.jpeg);");
        break;
    }
}


QString Dialog::gaintime()
{
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_time =current_date_time.toString("yyyy.MM.dd hh:mm:ss ddd");
    return current_time;
}

void Dialog::switchuicallback()
{
        switch (page) {
        case 0: //bedroom
            if(bed_room2_light==true) {
                ui->Button_close_light->show();
                ui->Button_open_light->close();
            }
            else
            {
                ui->Button_close_light->close();
                ui->Button_open_light->show();
            }
            break;
        case 1:
            if(livingroom_light==true) {
                ui->Button_close_light->show();
                ui->Button_open_light->close();
            }
            else
            {
                ui->Button_close_light->close();
                ui->Button_open_light->show();
            }
            break;
        case 2:
            if(kitchen_light==true) {
                ui->Button_close_light->show();
                ui->Button_open_light->close();
            }
            else
            {
                ui->Button_close_light->close();
                ui->Button_open_light->show();
            }
            break;
        case 3:
            if(bed_room1_light==true) {
                ui->Button_close_light->show();
                ui->Button_open_light->close();
            }
            else
            {
                ui->Button_close_light->close();
                ui->Button_open_light->show();
            }

            break;
        }
}

void Dialog::deal_key()
{
    if(!(kbdfd<0))//按键驱动可用，读取按键状态
    {
        read(kbdfd,&KbdStatus,1);
        if(Last_key_value == KbdStatus)  //按一下触发一次
        {
            return;
        }

        switch(KbdStatus)
        {
            case 1: //厨房界面
                Last_key_value = 1;
                ui->stackedWidget->setCurrentIndex(2);
                if(kitchen_light==false)
                {
                    kitchen_light=true;
                    setpage_on(KITCHEN);
                    sql_insert_data("本地操作", "打开厨房灯");
                    a_dr.LEDsSetValue(1,0);
                }
                else
                {
                    kitchen_light=false;
                    setpage_off(KITCHEN);
                    sql_insert_data("本地操作", "关闭厨房灯");
                    a_dr.LEDsSetValue(0,0);
                }
                break;
            case 2: //客厅界面
                Last_key_value = 2;
                ui->stackedWidget->setCurrentIndex(1);
                if(livingroom_light==false)
                {
                    livingroom_light=true;
                    setpage_on(LIVROOM);
                    sql_insert_data("本地操作", "打开客厅灯");
                    a_dr.LEDsSetValue(1,1);
                }
                else
                {
                    livingroom_light=false;
                    setpage_off(LIVROOM);
                    sql_insert_data("本地操作", "关闭客厅灯");
                    a_dr.LEDsSetValue(0,1);
                }
                break;
            case 4: //主卧界面
                Last_key_value = 4;
                ui->stackedWidget->setCurrentIndex(3);
                if(bed_room1_light==false)
                {
                    bed_room1_light=true;
                    setpage_on(BEDROOM1);
                    sql_insert_data("本地操作", "打开主卧灯");
                    a_dr.LEDsSetValue(1,2);
                }
                else
                {
                    bed_room1_light=false;
                     setpage_off(BEDROOM1);
                    sql_insert_data("本地操作", "关闭主卧灯");
                    a_dr.LEDsSetValue(0,2);
                }
                break;
            case 8: //次卧界面
                Last_key_value = 8;
                ui->stackedWidget->setCurrentIndex(0);
                if(bed_room2_light==false)
                {   bed_room2_light=true;
                    setpage_on(BEDROOM2);
                    sql_insert_data("本地操作", "打开次卧灯");
                    a_dr.LEDsSetValue(1,3);
                }
                else
                {   bed_room2_light=false;
                    setpage_off(BEDROOM2);
                    sql_insert_data("本地操作", "关闭次卧灯");
                    a_dr.LEDsSetValue(0,3);
                }
                break;
            default:
                Last_key_value = 0;
                break;

        }


    }
}


void Dialog::sql_insert_data(QString type, QString message)  //插入数据库的函数
{
    current_time=gaintime();
    w2dba record;
    record.id=1; //设置自增了，所以这个值不修改
    record.date=current_time;
    record.type=type;
    record.message=message;
    MyDB_light.singleinsertdata(tablename,record);//插入一条数据
    queryTable();

}


void Dialog::on_Button_open_light_clicked()
{
    int index=ui->stackedWidget->currentIndex();
    switch(index)
    {
        case 2: //厨房界面
            if(kitchen_light==false)
            {
                kitchen_light=true;
                setpage_on(KITCHEN);
                sql_insert_data("本地操作", "打开厨房灯");
                a_dr.LEDsSetValue(1,0);
            }
            break;
        case 1: //客厅界面
            if(livingroom_light==false)
            {
                livingroom_light=true;
                setpage_on(LIVROOM);
                sql_insert_data("本地操作", "打开客厅灯");
                a_dr.LEDsSetValue(1,1);
            }
            break;
        case 3: //主卧界面
            if(bed_room1_light==false)
            {
                bed_room1_light=true;
                setpage_on(BEDROOM1);
                sql_insert_data("本地操作", "打开主卧灯");
                a_dr.LEDsSetValue(1,2);
            }
            break;
        case 0: //次卧界面
            if(bed_room2_light==false)
            {   bed_room2_light=true;
                setpage_on(BEDROOM2);
                sql_insert_data("本地操作", "打开次卧灯");
                a_dr.LEDsSetValue(1,3);
            }
            break;
    }
    queryTable();
}

void Dialog::on_Button_close_light_clicked()
{
    int index=ui->stackedWidget->currentIndex();
    switch(index)
    {
        case 2: //厨房界面
            if(kitchen_light==true)
            {
                kitchen_light=false;
                setpage_off(KITCHEN);
                sql_insert_data("本地操作", "关闭厨房灯");
                a_dr.LEDsSetValue(0,0);
            }
            break;
        case 1: //客厅界面
            if(livingroom_light==true)
            {
                livingroom_light=false;
                setpage_off(LIVROOM);
                sql_insert_data("本地操作", "关闭客厅灯");
                a_dr.LEDsSetValue(0,1);
            }
            break;
        case 3: //主卧界面
            if(bed_room1_light==true)
            {
                bed_room1_light=false;
                setpage_off(BEDROOM1);
                sql_insert_data("本地操作", "关闭主卧灯");
                a_dr.LEDsSetValue(0,2);
            }
            break;
        case 0: //次卧界面
            if(bed_room2_light==true)
            {
                bed_room2_light=false;
                setpage_off(BEDROOM2);
                sql_insert_data("本地操作", "关闭次卧灯");
                a_dr.LEDsSetValue(0,3);
            }
            break;
    }
    queryTable();
}

void Dialog::on_Button_go_kitchen__clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
}

void Dialog::on_Button_go_livingroom_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void Dialog::on_Button_bed_room2_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void Dialog::on_Button_bed_room1_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
}

void Dialog::on_pushButton_record_clicked()
{
    ui->stackedWidget->setCurrentIndex(4);
}


void Dialog::on_pushButton_return_clicked()
{
    light_arr[0] = kitchen_light;
    light_arr[1] = livingroom_light;
    light_arr[2] = bed_room2_light;
    light_arr[3] = bed_room1_light;

    KbdScanTimer->stop();

    this->parentWidget()->show();
    delete this;
}

void Dialog::on_pushButton_query_clicked()
{
    QString Query_mode , str ,text,Query_text;

    if(ui->comboBox->currentIndex() == 0)
    {
        queryTable();
        return;
    }
    else if(ui->comboBox->currentIndex() == 1)
    {
        Query_mode = "ID";
        Query_text =ui->lineEdit_query_text->text();
    }
    else if(ui->comboBox->currentIndex() == 2)
    {
        Query_mode = "DATE";
        text=ui->lineEdit_query_text->text();
        Query_text = QString("%%%1%%").arg(text);
    }
    else if(ui->comboBox->currentIndex() == 3)
    {
        Query_mode = "TYPE";
        text=ui->lineEdit_query_text->text();
        Query_text = QString("%%%1%%").arg(text);
    }
    else if(ui->comboBox->currentIndex() == 4)
    {
        Query_mode = "MESSAGE";
        text=ui->lineEdit_query_text->text();
        Query_text = QString("%%%1%%").arg(text);
    }


    str = QString("select *from EVENTTABLE_light WHERE %1 like '%2'").arg(Query_mode).arg(Query_text);
    qDebug()<<"str="<<str;
    sqlmodel.setQuery(str);
    ui->tableView->setModel(&sqlmodel);
}



void Dialog::on_pushButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(page=(ui->stackedWidget->currentIndex()+3)%4);
}

void Dialog::on_pushButton_2_clicked()
{
    ui->stackedWidget->setCurrentIndex(page=(ui->stackedWidget->currentIndex()+1)%4);
}
